﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.Data;

using System.Windows.Input;

namespace HA.ModFlow.Application
{
    public class MiniGridViewModel : HViewModel
    {

        public MiniGridViewModel(Grid3d data)
        {
            this.Grid = data;

            if (!ProjectService.DefaultService.BNullProj)
            {

                this._currentViewPort = ProjectService.DefaultService.Project.CurrentViewPort;

                this._viewMode = this._currentViewPort.ViewType;

                this._lnFront = this._currentViewPort.ViewFrontIndex;
                this._lnRight = this._currentViewPort.ViewRightIndex;
                this._lnTop = this._currentViewPort.ViewLayerIndex;

                this.CurrentNumber = this._currentViewPort.Number;

                switch (_viewMode)
                {
                    case ProjectViewType.TOP:
                        _viewModeInt = 0;
                        break;
                    case ProjectViewType.FRONT:
                        _viewModeInt = 1;
                        break;
                    case ProjectViewType.RIGHT:
                        _viewModeInt = 2;
                        break;
                    default:
                        _viewModeInt = 0;
                        break;
                }
            }






        }


        ProjectViewPort _currentViewPort;


        bool _enable;

        public bool Enable
        {
            get { return _enable; }
            set { _enable = value; }
        }


        Grid3d _grid;

        public Grid3d Grid
        {
            get { return _grid; }
            set
            {
                bool isOrignalNull = _grid == null ? true : false;
                _grid = value;

                if (this._grid == null)
                {
                    this._enable = false;

                    this.OnPropertyChanged("Enable");
                    this.ViewMode = ProjectViewType.TOP;
                    this._lnFront = 1;
                    this._lnRight = 1;
                    this._lnTop = 1;
                    this.LayerCount = 0;

                    this.OnPropertyChanged("CurrentNumber");
                }
                else
                {
                    this._enable = true;

                    if (isOrignalNull)
                    {
                        this.ViewMode = ProjectViewType.TOP;
                        this._lnFront = 1;
                        this._lnRight = 1;
                        this._lnTop = 1;
                    }

                    this.OnPropertyChanged("Enable");

                    switch (_viewMode)
                    {
                        case ProjectViewType.TOP:
                            this.LayerCount = _grid.ModFlowGrid.LayersCount;
                            break;
                        case ProjectViewType.FRONT:
                            this.LayerCount = _grid.ModFlowGrid.RowsCount;
                            break;
                        case ProjectViewType.RIGHT:
                            this.LayerCount = _grid.ModFlowGrid.ColsCount;
                            break;
                        default:
                            break;
                    }

                    this.OnPropertyChanged("CurrentNumber");
                }
            }
        }


        int _viewModeInt;

        public int ViewModeInt
        {
            get { return _viewModeInt; }

            set
            {
                this._viewModeInt = value;
                this.OnPropertyChanged("ViewModeInt");
            }

        }

        int _layerCount;

        public int LayerCount
        {
            get { return _layerCount; }
            set
            {
                _layerCount = value;
                OnPropertyChanged("LayerCount");
            }
        }

        ProjectViewType _viewMode;

        public ProjectViewType ViewMode
        {
            get { return _viewMode; }
            set
            {

                _viewMode = value;

                if (this._currentViewPort != null)
                    this._currentViewPort.ViewType = this._viewMode;

                switch (this._viewMode)
                {
                    case ProjectViewType.TOP:

                        Render3dModule.DefaultModule.SetView(HAVTKPack.VIEWTYPE.TOP);
                        GisModule.DefaultModule.GisManager.VIEWMODE = HAGISV2.FMAPVIEW.NORMAL;

                        this._desc = "层(k):";
                        this._viewModeInt = 0;
                        if (this._grid != null)
                            this._layerCount = _grid.ModFlowGrid.LayersCount;

                        break;


                    case ProjectViewType.FRONT:

                        Render3dModule.DefaultModule.SetView(HAVTKPack.VIEWTYPE.FRONT);
                        GisModule.DefaultModule.GisManager.VIEWMODE = HAGISV2.FMAPVIEW.FRONT;
                        this._desc = "行(i):";
                        this._viewModeInt = 1;
                        if (this._grid != null)
                            this._layerCount = _grid.ModFlowGrid.RowsCount;

                        break;


                    case ProjectViewType.RIGHT:
                        Render3dModule.DefaultModule.SetView(HAVTKPack.VIEWTYPE.RIGHT);
                        GisModule.DefaultModule.GisManager.VIEWMODE = HAGISV2.FMAPVIEW.RIGHT;

                        this._desc = "列(j):";
                        this._viewModeInt = 2;
                        if (this._grid != null)
                            this._layerCount = _grid.ModFlowGrid.ColsCount;

                        break;


                    default:
                        break;
                }


                this.OnPropertyChanged("Desc");
                this.OnPropertyChanged("ViewModeInt");
                this.OnPropertyChanged("CurrentNumber");
                this.OnPropertyChanged("LayerCount");
            }
        }



        int _lnRight = 1;

        public int LnRight
        {
            get { return _lnRight; }
            set
            {
                if (this._grid == null)
                {
                    _lnRight = 1;
                    return;
                }
                if (this._lnRight != value)
                {
                    if (value > this._grid.ModFlowGrid.ColsCount)
                    {
                        value = this._grid.ModFlowGrid.ColsCount;

                    }

                    if (value < 1)
                    {
                        value = 1;

                    }

                    _lnRight = value;

                    this.OnRightNumChanged();

                }


            }
        }



        int _lnFront = 1;

        public int LnFront
        {
            get { return _lnFront; }
            set
            {
                if (this._grid == null)
                {
                    _lnFront = 1;
                    return;
                }

                if (this._lnFront != value)
                {
                    if (value > this._grid.ModFlowGrid.RowsCount)
                    {
                        value = this._grid.ModFlowGrid.RowsCount;

                    }

                    if (value < 1)
                    {
                        value = 1;

                    }

                    _lnFront = value;

                    this.OnFrontNumChanged();

                }

            }
        }

        int _lnTop = 1;

        public int LnTop
        {
            get { return _lnTop; }
            set
            {
                if (this._grid == null)
                {
                    _lnTop = 1;
                    return;
                }

                if (this._lnTop != value)
                {
                    if (value > this._grid.ModFlowGrid.LayersCount)
                    {
                        value = this._grid.ModFlowGrid.LayersCount;

                    }

                    if (value < 1)
                    {
                        value = 1;

                    }

                    _lnTop = value;

                    this.OnTopNumChanged();


                }


            }
        }



        protected void OnTopNumChanged()
        {
            GisModule.DefaultModule.SetGridLayerNum(this._lnTop);

        }

        protected void OnRightNumChanged()
        {

            GisModule.DefaultModule.SetGridLayerNum(this._lnRight);

        }


        protected void OnFrontNumChanged()
        {
            GisModule.DefaultModule.SetGridLayerNum(this._lnFront);

        }



        string _desc = "层(k)";

        public string Desc
        {
            get { return _desc; }
            set
            {
                _desc = value;
                OnPropertyChanged("Desc");
            }
        }



        public int CurrentNumber
        {
            get
            {

                switch (this._viewMode)
                {
                    case ProjectViewType.TOP:

                        return this._lnTop;


                    case ProjectViewType.FRONT:
                        return this._lnFront;


                    case ProjectViewType.RIGHT:

                        return this._lnRight;


                    default:
                        break;
                }

                return 1;


            }

            set
            {


                switch (this._viewMode)
                {
                    case ProjectViewType.TOP:


                        this._desc = "层(k)";
                        this.LnTop = value;

                        if (_currentViewPort != null)
                            this._currentViewPort.Number = this.LnTop;
                        if (this._grid != null)
                            this._layerCount = this._grid.ModFlowGrid.LayersCount;

                        break;


                    case ProjectViewType.FRONT:


                        this._desc = "行(i)";
                        this.LnFront = value;

                        if (_currentViewPort != null)
                            this._currentViewPort.Number = this.LnFront;
                        if (this._grid != null)
                            this._layerCount = this._grid.ModFlowGrid.RowsCount;

                        break;


                    case ProjectViewType.RIGHT:


                        this._desc = "列(j)";
                        this.LnRight = value;

                        if (_currentViewPort != null)
                            this._currentViewPort.Number = this.LnRight;
                        if (this._grid != null)
                            this._layerCount = this._grid.ModFlowGrid.ColsCount;

                        break;


                    default:
                        break;
                }



                this.OnPropertyChanged("LayerCount");
                this.OnPropertyChanged("CurrentNumber");


            }
        }


        #region Command

        ICommand _upLayer;

        public ICommand UpLayer
        {
            get
            {

                if (this._upLayer == null)
                {
                    this._upLayer = new RelayCommand(m => this.CurrentNumber += 1);

                }





                return _upLayer;

            }



            set { _upLayer = value; }
        }


        ICommand _downLayer;

        public ICommand DownLayer
        {
            get
            {

                if (this._downLayer == null)
                {
                    this._downLayer = new RelayCommand((m) => this.CurrentNumber -= 1);

                }

                return _downLayer;
            }
            set { _downLayer = value; }
        }



        #endregion




    }
}
